/*
 * FileName: AdminController.java
 * Author:   庾宗耀
 * Date:     2015年1月29日 上午9:50:22
 * Description: 管理员添加、修改用户等的实现
 * History:
 * <author>   <time>    <version>    <desc>
 * 修改人姓名                 修改时间                 版本号                               描述
 */
package com.linkage.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.admin.interceptor.NeedAdminLogin;
import com.linkage.admin.service.AdminService;
import com.linkage.book.pojo.Book;
import com.linkage.category.pojo.Category;
import com.linkage.category.service.CategoryService;
import com.linkage.user.pojo.User;

/**
 * @author 庾宗耀
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @NeedAdminLogin
    public String list(Model model) {
        List<User> users = this.adminService.queryAll();
        model.addAttribute("users", users);

        return "user/list";
    }

    /**
     * 用户更新功能
     */
    @RequestMapping(value = "/updateView/{userId}", method = RequestMethod.GET)
    @NeedAdminLogin
    public String updateView(Model model, @PathVariable("userId") int userId) {
        User user = this.adminService.findUserById(userId);
        model.addAttribute("user", user);

        return "user/updateView";
    }

    /**
     * 
     * @param user
     * @return list.jsp
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @NeedAdminLogin
    public String update(User user) {
        this.adminService.update(user);
        return "user/deleteSuccess";
    }
    
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    @NeedAdminLogin
    public String delete(User user, @PathVariable("userId") String userId) {
        this.adminService.delete(userId);

        return "/user/deleteSuccess";
    }
    
    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    @NeedAdminLogin
    public String addView() {
        return "user/addView";
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @NeedAdminLogin
    public String save(User user) {
        this.adminService.save(user);
        return "redirect:/user/list";
    }
    
    @RequestMapping(value="/addBookView", method = RequestMethod.GET)
    @NeedAdminLogin
    public String addBookView() {
    	return "book/addBookView";
    }
    
    @RequestMapping(value="/addBook",method = RequestMethod.POST)
    @NeedAdminLogin
    public String addBook(Book book) {
    	adminService.addBook(book);
    	return "book/addBookSuccess";
    }

    @RequestMapping(value="/bookList",method=RequestMethod.GET)
    @NeedAdminLogin
    public String bookList(Model model) {
        List<Book> books = this.adminService.queryAllBook();
        model.addAttribute("books", books);

    	return "book/bookList";
    }
    
    @RequestMapping(value = "/deleteBook/{bookId}", method = RequestMethod.GET)
    @NeedAdminLogin
    public String deleteBookById(@PathVariable("bookId") String bookId) {
        this.adminService.deleteBookById(bookId);

        return "book/deleteBookSuccess";
    }

    @RequestMapping(value = "/updateBook/{bookId}", method = RequestMethod.GET)
    @NeedAdminLogin
    public String updateBookByIdView(Model model,@PathVariable("bookId") String bookId) {
    	model.addAttribute("bookId", bookId);
        return "book/updateBookView";
    }
    
    @RequestMapping(value ="/updateBook", method = RequestMethod.POST)
    @NeedAdminLogin
    public String updateBook(HttpServletRequest req,Book book) {

    	this.adminService.updateBook(book);
        return "book/updateBookSuccess";
    }
    
    @RequestMapping(value="/gotoAdminLogin",method=RequestMethod.GET)
    public String gotoAdminLogin() {
    	return "admin/gotoAdminLogin";
    }
    
    @RequestMapping(value="/doAdminLogin",method=RequestMethod.POST)
    public String doAdminLogin(@RequestParam("userName") String userName, @RequestParam("password") String password,
            HttpServletRequest request, Model model) {
        boolean login = this.adminService.adminLogin(request, userName, password);
        if (login) {
            return "admin/admin";
        } else {
            model.addAttribute("errorMsg", "登录失败！");
            return "login/login_form";
        }
    }
    
    @RequestMapping(value="/home",method = RequestMethod.GET)
    public String home() {
    	return "admin/admin";
    }
    
    @RequestMapping(value="/top",method = RequestMethod.GET)
    public String top() {
    	return "menu/top";
    }
    
    @RequestMapping(value="/left",method = RequestMethod.GET)
    public String left() {
    	return "menu/left";
    }
    
    @RequestMapping(value="/right",method = RequestMethod.GET)
    public String right() {
    	return "menu/right";
    }
    
    
    //添加书的类别view
    @RequestMapping(value="/addCategoryView", method = RequestMethod.GET)
    public String addCategoryView() {
    	return "/category/addCategoryView";
    }
    
    //添加书的类别
    @RequestMapping(value="/addCategory", method = RequestMethod.POST)
    public String addCategory(Category category) {
    	this.adminService.addCategory(category);
    	return "category/addCategorySuccess";
    }
    
    //删除书的类别
    @RequestMapping(value="/deleteCategory/{categoryId}", method = RequestMethod.GET)
    public String deleteCategoryById(@PathVariable("categoryId") int categoryId) {
    	this.adminService.deleteCategoryById(categoryId);
    	return "success";
    }
    
  //修改书的类别View
    @RequestMapping(value="/updateCategoryView/{categoryId}", method = RequestMethod.GET)
    public String UpdateCategoryView(HttpServletRequest request, Model model,@PathVariable("categoryId") int categoryId) {
//    	request.setAttribute("categoryId", categoryId);
    	Category category = this.categoryService.loadOne(categoryId);
    	model.addAttribute("category", category);
    	return "category/updateCategoryView";
    }
    
  //修改书的类别
    @RequestMapping(value="/updateCategory", method = RequestMethod.POST)
    public String UpdateCategory(Category category) {
    	this.adminService.UpdateCategory(category);
    	return "category/updateCategorySuccess";
    }
    
    //查询书的所有类别
    @RequestMapping(value="/categoryList", method=RequestMethod.GET)
    public String queryAllCategory(Model model) {
    	List<Category> categoryList = this.categoryService.queryAll();
    	model.addAttribute("categoryList", categoryList);
    	return "category/categoryList";
    }
}
